<template>
  <div class="login-container">
    <div class="login-logo">
      <h1>
        <a href="#">
          <img :src="require('../../../static/images/login/login_logo.png')" alt="">
        </a>
        <i class="login-bar"></i>
        <i class="login-title">登录</i>
      </h1>
    </div>
    <div class="login-wrapper">
      <div class="wrapper-layout">
        <div class="wrapper-center">
          <div class="wrapper-des">
            <img src="../../../static/images/login/login_text.png" width="470" height="144" alt="">
            <!--<img src="../../../static/images/login/logo_white_text.png" width="206" height="36" alt="">-->
          </div>
          <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" label-position="left">
            <el-form-item>
              <h1 style="text-align: center;color: rgb(90,90,90);font-size:20px;height: 60px;font-weight: 600;">
                用户登录
              </h1>
            </el-form-item>
            <el-form-item prop="username">
              <!--<span class="svg-container">-->
                <!--<svg-icon icon-class="user" />-->
              <!--</span>-->
              <el-input
                ref="username"
                v-model="loginForm.username"
                placeholder="请输入账号"
                name="username"
                type="text"
                tabindex="1"
                auto-complete="on"
                @keyup.tab.native="changeFocus('password',$event)"
                @keyup.enter.native="nextFocus('password',$event)"
              />
            </el-form-item>

            <el-form-item prop="password">
              <!--<span class="svg-container">-->
                <!--<svg-icon icon-class="password" />-->
              <!--</span>-->
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="请输入密码"
                name="password"
                tabindex="2"
                auto-complete="on"
                @keyup.tab.native="changeFocus('username',$event)"
                @keyup.enter.native="nextFocus('loginFlag',$event)"
              />
              <!--<span class="show-pwd" @click="showPwd">-->
                <!--<svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />-->
              <!--</span>-->
            </el-form-item>

            <el-form-item prop="Flag" class="flag" hidden="hidden">
              <!--<span class="svg-container">-->
                <!--<svg-icon icon-class="link" />-->
              <!--</span>-->
              <el-select v-model="loginForm.Flag" ref="loginFlag" placeholder="请选择系统"  @keyup.enter.native="handleLogin">
                <el-option label="系统一" value="系统一"></el-option>
                <el-option label="系统二" value="系统二"></el-option>
              </el-select>
              
            </el-form-item>
            <el-button :loading="loading" ref="login" type="primary" class="login" @keyup.enter.native="handleLogin" @click.native.prevent="handleLogin">登录</el-button>
            <el-form-item >
              <a href="#" target="_blank" class="user-manual">&lt;&lt;金民电子殡葬管理系统-用户手册&gt;&gt;</a>
              <router-view to="/404"></router-view>
            </el-form-item>
          </el-form>
        </div>

      </div>
    </div>
    <div class="footer">
      技术支持：济南金民电子 客服热线：0531-0000 官方网站：www.jinmdz.com
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { constantRoutes } from '@/router'
import defaultSettings from "../../../static/js/settings.js"
import { mapGetters } from 'vuex'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length ==0) {
        callback(new Error('请输入正确的用户名'))
      }else if (value.length > 50) {
        callback(new Error('用户名在50个字符之内'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 4 || value.length > 50) {
        callback(new Error('密码在4-50个字符之间'))
      } else {
        callback()
      }
    }
    const validateFlag = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请选择您要进入的系统'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: '',
        Flag:this.loginFlag
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      passwordClick:false,
      redirect: null,
      title:defaultSettings.title
    }
  },
  mounted(){
    console.log('index')
    if (this.$store.getters.loginFlag){
      this.loginForm.Flag = this.$store.getters.loginFlag
    }
  },
  // watch: {
  //   $route: {
  //     handler: function(route) {
  //
  //       this.redirect = (route.query && route.query.redirect)||'/login'
  //     },
  //     immediate: true
  //   }
  // },
  methods: {
    //切换聚焦点
    changeFocus(ref,e){
      e.target.blur()
      this.$refs[ref].focus();
    },
    nextFocus(ref,e){
      e.target.blur() // 或者this.$refs[row.id].blur()
      this.$refs[ref].focus();
      this.handleLogin()
    },
    showPwd() {
      if (!this.passwordClick) {
        this.passwordClick = true
        if (this.passwordType === 'password') {
          this.passwordType = ''
        } else {
          this.passwordType = 'password'
        }
        this.$nextTick(() => {
          this.$refs.password.focus()
        })
        this.passwordClick = false
      }

    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          console.log("登录",this.loginForm)
          this.$store.dispatch('user/login', this.loginForm).then((res) => {
            if (res){
              console.log(res);
              //获得省市区县
              this.$store.dispatch('user/getProvinceCityCounty').then((data1) =>{                   //获取页面及button权限
                console.log(data1);
               }).catch(()=>console.log('promise catch err'))
              //登陆用户
              this.$store.dispatch('user/getInfo').then((data) =>{                   //获取页面及button权限
                if (data&&data.length) {
                  let page=[].concat(data);
                  // console.log(page)
                  this.$store.dispatch('permission/generateRoutes',page).then((routes) =>{
                    this.$router.options.routes=(routes)
                    this.$router.$addRoutes(routes)
                       routes.map((item) => {
                          console.log(item);
                          if (!this.redirect) {
                            if (item.children) {
                              this.redirect = item.children[0].path;
                            }
                            return;
                          }
                        });
                    this.$router.push({ path: this.redirect || '/' })
                    //
                    this.loading = false
                  })
                }else{
                  this.loading = false
                }
              }).catch(()=>console.log('promise catch err'))
            } else {
              this.loading = false
            }

          }).catch(() => {
            this.loading = false
          })
        } else {
          // console.log('提交失败!!')
          return false
        }
      })
      this.loading = false
    }, //加载省市区县
      loadProvence(){

    }

  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#F8F9FB;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
.login-container .el-input__suffix-inner{
  padding-right: 15px;
}
.login-container .el-form-item__error {
  padding-left: 2em!important;
}
/* reset element-ui css */
.login-container {
  background-color:white;
  background-size:cover;
  padding-top: 40px;
  .login-logo{
    width: 1270px;
    margin:0 auto;
    text-align: left;
    margin-bottom: 12px;
    line-height: 60px;
    height: 60px;
    .login-bar{
      width: 3px;
      height: 50px;
      background:#F6F9FF;
      display: inline-block;
      margin: 0 10px;
    }
    .login-title{
      font-size:24px;
      color:rgb(90,90,90);
      vertical-align: text-bottom;
    }
    img{
      width: 110px;
    }
  }
  .login-wrapper{
    width: 100%;
    height: 620px;
    margin: 0 auto;
    position: relative;
    padding-top:60px;
    .wrapper-layout{
      position: absolute;
      z-index: 1;
      top: 0;
      left: 0;
      background:url("../../../static/images/login/login_bg.jpg");
      background-size: cover;
      background-repeat: no-repeat;
      background-position: 50%;
      width: 100%;
      height: 100%;
      .wrapper-center{
        width: 1270px;
        height: 100%;
        margin: 0 auto;
        position: relative;
      }
      .wrapper-des{
        position: absolute;
        left: 40px;
        top:50%;
        margin-top:-90px;
      }
      .login-form {
        position: absolute;
        top: 15px;
        bottom: 30px;
        right: 0px;
        width: 426px;
        background:url("../../../static/images/login/login_box.png");
        background-size: cover;
        background-repeat: no-repeat;
        background-position: 50%;
        padding: 80px 45px 0 45px;
        margin: 0 auto;
        overflow: hidden;
        box-sizing: border-box;
        .login{
          width:100%;
          margin-top:20px;
          font-size:16px;
          border-radius: 20px;
        }
        .user-manual{
          font-size:14px;
          color:rgba(52, 161, 255, 1);
          font-weight: 600;
        }
      }
    }
    .title{
      letter-spacing: 1px;
      font-size:40px;
      font-weight: 700;
      text-align: center;
      color:rgba(52, 161, 255, 1);
    }
    .el-input {
      display: inline-block;
      height: 40px;
      width: 100%;
      input {
        font-size: 16px;
        font-family: "MicrosoftYaHei","微软雅黑";
        border: 1px solid rgba(123, 121, 121, 0.5);
        -webkit-appearance: none;
        border-radius: 0px;
        color: #000;
        background-color: #F8F9FB;
        height: 40px;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: #000 !important;
        }
      }
    }
    .el-select{
      width:100%;
    }
  }
  .login-wrapper .flag .el-input{
    width:100%;
  }

  .el-form-item {
    color: #454545;
    margin-bottom: 20px;
  }
  .el-form-item:first-of-type {
    border-bottom: 0px solid rgba(123, 121, 121, 1);
  }
  .el-form-item__error{
    padding-left:4em;
  }
}
  .footer{
    margin-top:45px;
    font-size:14px;
    color: #9c9c9c;
    text-align: center;
  }
</style>

<style lang="scss" scoped>
$bg:#fff;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;


  .tips {
    font-size: 16px;
    color: #999999;
    margin-bottom: 20px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

}

</style>
<style>
  .login-container .el-input__suffix{
    right: -12px;
  }
</style>
